﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using DeansWsccProject.App_Code;

namespace DeansWsccProject
{
    public partial class upload_ebook : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (User.IsInRole("subscribers"))
            {
                Response.Redirect("~/authors-only.aspx");
            }

            populateCoAuthorsListBox(); // Populate Co-Authors List Box with all users assigned to the 'authors' role, except the logged in author
        }
        
        protected void btnUploadEBook_Click(object sender, EventArgs e)
        {
            // Upload ebook front cover jpg file if it is submitted
            string newEBookJpgFilename = uploadEBookJpgFile();

            // Upload ebook pdf file
            string newEBookPdfFilename = uploadEBookPdfFile();

            insertNewEBookIntoEBooksTable(newEBookJpgFilename, newEBookPdfFilename);

            // Associate logged-in user ID with e-book ID in the author/e-book junction table
            insertEBookAuthorFkidsIntoJunctionTable(User.Identity.Name);

            // Associate selected co-authors in listbox with e-book id in the ebook/author junction table
            foreach (ListItem author in lbCoAuthors.Items)
            {
                if (author.Selected)
                {
                    insertEBookAuthorFkidsIntoJunctionTable(author.Text);
                }
            }

            Response.Redirect("~/profile.aspx");
        }

        

        private void populateCoAuthorsListBox()
        {
            string[] authorsUserNames = functions.getDataFromTable("SELECT UserName FROM userNameRoleView WHERE RoleName = @f1", "authors");
            foreach (string name in authorsUserNames)
            {
                if (name != User.Identity.Name) // don't add logged-in user
                {
                    lbCoAuthors.Items.Add(name);
                }
            }
        }

        private string uploadEBookJpgFile()
        {
            string newEBookJpgFilename = functions.getUniqueFilename("jpg");
            if (fuEBookJpg.HasFile)
            {
                fuEBookJpg.SaveAs(Request.MapPath("eBookImages") + "/" + newEBookJpgFilename);
            }
            return newEBookJpgFilename;
        }

        private string uploadEBookPdfFile()
        {
            string newEBookPdfFilename = functions.getUniqueFilename("pdf");
            fuEBookPdf.SaveAs(Request.MapPath("eBooks") + "/" + newEBookPdfFilename);
            return newEBookPdfFilename;
        }

        private void insertNewEBookIntoEBooksTable(string newEBookJpgFilename, string newEBookPdfFilename)
        {
            string insertEBookSqlStr = "INSERT INTO eBooks(eBookTitle,eBookDescription,eBookDownloadUrl,eBookReleaseDate,eBookImage) VALUES(@f1,@f2,@f3,@f4,@f5)";
            object[] parameters = { tbEBookTitle.Text, tbEBookDescription.Text, "eBooks/" + newEBookPdfFilename, DateTime.Now.Date, null };
            if (fuEBookJpg.HasFile) // replace null @f5 parameter value (i.e. eBookImage field) with one of following...
            {
                parameters[4] = "eBookImages/" + newEBookJpgFilename; // use unique filename if user has uploaded a file
            }
            else
            {
                parameters[4] = "eBookImages/default.jpg"; // use default pic if user has not uploaded a file
            }
            functions.putDataInTable(insertEBookSqlStr, parameters);
        }

        private void insertEBookAuthorFkidsIntoJunctionTable(object userName)
        {
            object[] parameters = { functions.getLastIdInserted("eBooks"), functions.getDataFromTable("SELECT ID FROM profile WHERE profileUserName = @f1", userName)[0] };
            functions.putDataInTable("INSERT INTO eBookAuthorJunction(eBook_fkid,profile_fkid) VALUES(@f1,@f2)", parameters);
        }
    }
}